package com.hzzc.core.controller.back;

import com.hzzc.BizException;
import com.hzzc.Result;
import com.hzzc.core.model.SSORole;
import com.hzzc.core.service.UserRoleService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/back")
public class BackUserRoleController {

    @Autowired
    private UserRoleService userRoleService;

    @GetMapping("/userRoles")
    public Result<Set<Long>> userRoles(@RequestParam("userId") long userId, @RequestParam("appId") long appId) {
        Set<Long> userRoleId = userRoleService.userRoleInThisApp(userId, appId).stream().map(SSORole::getId).collect(Collectors.toSet());
        return Result.success(userRoleId, "");
    }

    @PostMapping("/userRole")
    public Result<List<SSORole>> updateUserRole(@RequestBody UserRoleBean userRoleBean) throws BizException {
        userRoleService.updateUserRole(userRoleBean.appId, userRoleBean.userId, userRoleBean.roleIds);
        return Result.success(userRoleService.userRoleInThisApp(userRoleBean.appId, userRoleBean.userId), "");
    }

    @Data
    static class UserRoleBean {

        private long appId;

        private long userId;

        private Set<Long> roleIds;

    }

}
